<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <?php
        include("import.php");
        include("../service/imageService.php");
        include("../service/dishService.php");

        // prevent sql injection
        $url = ($_SERVER['REQUEST_URI']);
        if (!preg_match('/^id=[1-9]{1,}[0-1]*$/', substr($url, strrpos($url, "?") + 1, strlen($url))))
            echo "<script>window.location.href='index.php';</script>";

        $dishId = $_GET['id'];
        $result = getDishById($dishId);
        if (ConnectionManager::getInstance()->getNumRows() == 0)
            echo "<script>window.location.href='index.php';</script>";
        ?>
        <title><?php echo $result["DISH_NAME"]; ?></title>
    </head>
    <body>
        <div id="wrapper">
            <div id="wrapperContent">
                <?php include("header.php"); ?>

                <div id="wrapperMainContent">
                    <div id="dishDetailContent">
                        <div id="dish-detail-header-bg"></div>
                        <div id="dish-detail-body-bg">
                            <div id="leftContent">
                                <?php
                                $imagePath = "../img/product/";
                                $noImagePath = "../img/product/no-image.png";

                                $imageResult = getImagesByDishID($dishId);
                                $numRow = ConnectionManager::getInstance()->getNumRows();
                                $imagePaths = array();
                                $count = 0;

                                while ($row = ConnectionManager::getInstance()->fetchArray()) {
                                    $imagePaths[$count] = $row["IMG_PATH"];
                                    $count++;
                                }
                                if ($numRow == 0)
                                    echo "<center><img src=\"" . $noImagePath . "\"/></center>";
                                else
                                    echo "<img width=\"279\" height=\"259\" src=\"" . $imagePath . $imagePaths[0] . "\"/>";
                                ?>
                                <span>Click để xem ảnh lớn</span><br/>
                                <div id="imageViewer">
                                    <?php
                                    for ($i = 1; $i < count($imagePaths); $i++) {
                                        echo "<a href=\"" . $imagePath . $imagePaths[$i] . "\"><img src=\"" . $imagePath . $imagePaths[$i] . "\"/></a>";
                                    }
                                    ?>
                                </div>
                            </div>
                            <div id="rightContent">
                                <span id="dishName"><?php echo $result["DISH_NAME"]; ?></span>
                                <span><?php echo number_format($result["DISH_PRICE"]."000", 0, ",", "."); ?>đ</span>
                                <span>Mô tả:</span>
                                <span><?php echo $result["DISH_DESCRIPTION"]; ?></span>
                            </div>	
                        </div>
                        <div id="dish-detial-footer-bg"></div>
                    </div>
                </div>
            </div>
        </div>
        <?php include("footer.php"); ?>
    </body>
    <?php ConnectionManager::getInstance()->closeConnection(); ?>
</html>